/// 代表漫画图片的类

YAHOO.namespace("solor.comic");

/// 大图

YAHOO.solor.comic.Picture = function(container)
{
    var self = this;
    container = this.container = container; 
    var oContainer = this.oContainer = YAHOO.util.Dom.get(container);
    var g = this.g = YAHOO.solor.comic.Global;
    
    // 显示图片
    this.show = function()
    {
        if (g.currentShowPage  == g.page) return true;
        pages = g.data.pages; 
        page = g.page;
        if(!pages[page])
        {
            YAHOO.solor.comic.showAlert("警告", "找不到图片啦");
            return false;
        }
        var imgsrc = pages[page]["o"];
        g.currentImgSrc = imgsrc;
        var img = new Image();
        img.onload = function()
        {
            oContainer.innerHTML = "<img id='comic_image' src='" + imgsrc + "' />"; 
            if (document.all)
            {
                g.bgWidth = document.body.clientWidth;
            }
            else
            {
                g.bgWidth = YAHOO.util.Dom.get("comic_background").clientWidth;
            }
            if (document.all)
            {
                g.bgHeight = document.body.clientHeight;
            }
            else
            {
                g.bgHeight = YAHOO.util.Dom.get("comic_background").clientHeight;
            }
            g.imgWidth = img.width;
            g.imgHeight= img.height;
            registerKeys();
            var be = new YAHOO.solor.comic.BirdsEye("comic_birdseye", imgsrc);
            be.show();
        }
        img.src=imgsrc;
    }
    // 注册快捷键
    function registerKeys()
    {
        if (YAHOO.solor.comic.Global.keyEvents) 
        {
          for(var i = 0; i < YAHOO.solor.comic.Global.keyEvents.length; i++)
          {
            YAHOO.solor.comic.Global.keyEvents[i].disable();
          }
        }
        YAHOO.solor.comic.Global.keyEvents = [];
        var single = g.shortKey['single'];
        var ctrl = g.shortKey['ctrl'];
        for(var i in single)
        {
            var action = single[i];
            registerEventByAction(i, action, false) 
        }
        
        for(var i in ctrl)
        {
            var action = single[i];
            registerEventByAction(i, action, true) 
        }
    
    }

    function registerEventByAction(key, action, isCtrl)
    {
        var k ;
        var callback = function (){};
        switch(action)
        {
            case "up" :
                callback = moveUp;  break;
            case "down":
                callback = moveDown; break;
            case "left":
                callback = moveLeft ; break;
            case "right":
                callback = moveRight ; break;
            case "pgdown":
                callback = pageDown ; break;
            case "pgup":
                callback = pageUp ; break;
                
        }
        k = new YAHOO.util.KeyListener(document, { ctrl: isCtrl, keys: key},
                {fn : callback, scope: YAHOO.solor.comic,correctScope:true});      
        k.enable();
        YAHOO.solor.comic.Global.keyEvents[YAHOO.solor.comic.Global.keyEvents.length] = k;
    }

    function moveUp(e)
    {
        var cp = YAHOO.util.Dom.get("comic_picture");
        var currentTop = YAHOO.util.Dom.getStyle(cp, "top");
        currentTop = currentTop.replace(/[^0-9\-]*/, "");
        if ( currentTop == "") currentTop = 0; 
        currentTop = parseInt(currentTop);
        currentTop = currentTop - g.xSteps;
        YAHOO.util.Dom.setStyle(cp, "top", currentTop + "px");
    }

    function moveDown(e)
    {
        var cp = YAHOO.util.Dom.get("comic_picture");
        var currentTop = YAHOO.util.Dom.getStyle(cp, "top");
        currentTop = currentTop.replace(/[^0-9\-]*/, "");
        if ( currentTop == "") currentTop = 0; 
        currentTop = parseInt(currentTop);
        currentTop = currentTop + g.xSteps;
        YAHOO.util.Dom.setStyle(cp, "top", currentTop + "px");
    }

    function moveLeft(e)
    {
        var cp = YAHOO.util.Dom.get("comic_picture");
        var currentLeft = YAHOO.util.Dom.getStyle(cp, "left");
        currentLeft = currentLeft.replace(/[^0-9\-]*/, "");
        if ( currentLeft == "") currentLeft = 0; 
        currentLeft = parseInt(currentLeft);
        currentLeft = currentLeft - g.ySteps;
        YAHOO.util.Dom.setStyle(cp, "left", currentLeft + "px");
    }

    function moveRight(e)
    {
        var cp = YAHOO.util.Dom.get("comic_picture");
        var currentLeft = YAHOO.util.Dom.getStyle(cp, "left");
        currentLeft = currentLeft.replace(/[^0-9\-]*/, "");
        if ( currentLeft == "") currentLeft = 0; 
        currentLeft = parseInt(currentLeft);
        currentLeft = currentLeft + g.ySteps;
        YAHOO.util.Dom.setStyle(cp, "left", currentLeft + "px");
    }

    function pageDown(e)
    {
        if (g.data.pages[g.page+1])
            g.page++;
        self.show();
    }

    function pageUp(e)
    {
        if (g.page > 1)
            g.page--;
        self.show();
    }

}

///鹰眼
/// container 这里是鹰眼需要绑定的对象
YAHOO.solor.comic.BirdsEye = function(container, imgsrc)
{
   var self = this;
   _dom = YAHOO.util.Dom;
   g = YAHOO.solor.comic.Global;
   var container = _dom.get(container);
   this.imgsrc = imgsrc;

   this.show = function()
   {
     container.style.display="block";
     this.refresh();
   }

   this.hide = function()
   {
     container.style.display="none";
   }

   this.refresh = function()
   {
     container.innerHTML = ""; //清空   
     this.olWidth = olWidth = g.screenWidth;
     this.olHeight = olHeight = Math.floor(g.bgHeight / g.bgWidth * g.screenWidth);
     this.sWidth = sWidth = Math.floor(g.imgWidth * (g.screenWidth/g.bgWidth));
     this.sHeight = sHeight = Math.floor(g.imgHeight * (g.screenWidth/g.bgWidth));
     this.cavWidth = (olWidth > sWidth) ? olWidth : sWidth;
     this.cavHeight = (olHeight > sHeight) ? olHeight : sHeight;
     //container.innerHTML = "w" + g.imgWidth + "h" + g.imgHeight + "<br>olw" + olWidth + "olh" + olHeight + "sw" + sWidth + "sh" + sHeight + "<br>cw" + this.cavWidth + "ch" + this.cavHeight; 


     var xoffsetLeft = (_dom.getX("comic_picture"));
     var yoffsetTop = (_dom.getY("comic_picture"));
     var left = -xoffsetLeft * (self.sWidth/g.imgWidth );
     var top = -yoffsetTop * (self.sHeight/g.imgHeight );
     
     container.style.top = left + "0px";
     container.style.left = top + "0px";
     

     container.style.width = this.cavWidth + "px";
     container.style.height = this.cavHeight + "px";
     container.innerHTML = "<div id='comic_be_screen' style='border:1px solid green;position: absolute;z-index:999;"
                            + "background-color:white; opacity:0.3; filter:alpha(opacity=30);cursor: hand' ></div>" 
                            + "<div id='comic_be_img' style='border:1px solid black;position: absolute;'></div>" ;
     this.beScreen = _dom.get("comic_be_screen");
     this.beImg = _dom.get("comic_be_img");

     this.beScreen.style.width = this.olWidth + "px";
     this.beScreen.style.height = this.olHeight + "px";

     this.beImg.style.width = this.sWidth + "px";
     this.beImg.style.height = this.sHeight + "px";

     var sImg = document.createElement("image");
     sImg.src = imgsrc;
     sImg.width = this.sWidth;
     sImg.height = this.sHeight;
     this.beImg.appendChild(sImg);
     this.dd = new YAHOO.util.DD(this.beScreen);
     this.dd.onDrag = this.onDrag;
     this.resetConstraint();

   }

   // 超出范围检测
   this.resetConstraint = function()
   {
     var xoffsetLeft = (_dom.getX(this.beScreen) - _dom.getX(container));
     var xoffsetRight = (this.cavWidth - this.olWidth) - (_dom.getX(this.beScreen) - _dom.getX(container));
     //alert(xoffsetRight + "x" + xoffsetRight);

     var yoffsetTop = (_dom.getY(this.beScreen) - _dom.getY(container));
     var yoffsetBottom = (this.cavHeight - this.olHeight) - (_dom.getY(this.beScreen) - _dom.getY(container));
     //alert(yoffsetTop + "y" + yoffsetBottom);

     this.dd.setXConstraint(xoffsetLeft, xoffsetRight);
     this.dd.setYConstraint(yoffsetTop, yoffsetBottom);
   }

   this.onDrag = function(e)
   {
     var xoffsetLeft = (_dom.getX(self.beScreen) - _dom.getX(container));
     var yoffsetTop = (_dom.getY(self.beScreen) - _dom.getY(container));
     var left = -xoffsetLeft * (g.imgWidth / self.sWidth);
     var top = -yoffsetTop * (g.imgHeight / self.sHeight);
     var cp = YAHOO.util.Dom.get("comic_picture");
     // _dom.get("comic_be_screen").innerHTML = (yoffsetTop + "+" + g.imgHeight+ "+" +self.sHeight +  "x" + top + "+<br>" + _dom.getY(self.beScreen));
     //_dom.get("comic_be_screen").innerHTML = (_dom.getY(self.beScreen) + "x" + _dom.getY(container));

     _dom.setStyle(cp, "left", left + "px");
     _dom.setStyle(cp, "top" , top + "px");

   }

   //this.refresh();
}
